const db = uniCloud.database()
const dbCmd = db.command;
module.exports = {
	main: async (event) => {
		let {
			res,
			data,
			action
		} = event;
		switch (action) {
			case 'query':
				res = await getTabbar(data);
				break;
			case 'edit':
				res = await editTabbar(data);
				break;
			default:
				res = {
					code: 402,
					msg: '非法访问'
				}
				break;
		}
		return res;
	}
}

/**
 * 获取导航列表
 */
async function getTabbar(data) {
	var res = {}
	var result = await db.collection('tabbar').where({})
		.get()
	res.code = 200
	res.data = result.data
	return res
}

/**
 * 更新导航
 */
async function editTabbar(data) {
	var res = {}
	const result = await db.collection('tabbar').doc(data.id).update({
		data: data.tabbarData,
		updatetime: data.updatetime
	})
	if (result.updated === 1) {
		res.code = 200;
		res.msg = '更新数据成功';
	} else {
		res.code = 402;
		res.msg = '保存失败，请重新操作'
	}
	return res
}
